<template>
  <el-form :inline="true" class="demo-form-inline" :model="cForm">
    <el-form-item label="一级分类">
      <el-select placeholder="请选择" value="" v-model="cForm.category1Id" @change="getC2List">
        <el-option :label="c1.name" :value="c1.id" v-for="c1 in c1List" :key="c1.id"></el-option>
      </el-select>
    </el-form-item>

    <el-form-item label="二级分类">
      <el-select placeholder="请选择" value="" v-model="cForm.category2Id" @change="getC3List">
        <el-option :label="c2.name" :value="c2.id" v-for="c2 in c2List" :key="c2.id"></el-option>
      </el-select>
    </el-form-item>

    <el-form-item label="三级分类">
      <el-select placeholder="请选择" value="" v-model="cForm.category3Id" @change="handler">
        <el-option :label="c3.name" :value="c3.id" v-for="c3 in c3List" :key="c3.id"></el-option>
      </el-select>
    </el-form-item>
  </el-form>
</template>

<script>
export default {
  name: "Category",
  data() {
    return {
      c1List : [],
      c2List : [],
      c3List : [],
      cForm: {
        category1Id : '',
        category2Id : '',
        category3Id : '',
      }
    }
  },
  mounted() {
    this.getC1List();
  },
  methods: {
    async getC1List(){
        let res = await this.$ajax.attr.reqGetCategory1();
        if (res.code == 200) {
          this.c1List = res.data;
        }
    },
    async getC2List() {
      this.c2List = [];
      this.c3List = [];
      this.cForm.category2Id = '';
      this.cForm.category3Id = '';

      const {category1Id} = this.cForm;
      this.$emit('getCategoryId', {id:category1Id,flag:1});

      let res = await this.$ajax.attr.reqGetCategory2(category1Id);
      if (res.code == 200) {
        this.c2List = res.data;
      }
    },
    async getC3List() {
      this.c3List = [];
      this.cForm.category3Id = '';

      const {category2Id} = this.cForm;

      this.$emit('getCategoryId', {id:category2Id,flag:2});

      let res = await this.$ajax.attr.reqGetCategory3(category2Id);

      if (res.code == 200) {
        this.c3List = res.data;
      }
    },
    handler() {
      const {category3Id} = this.cForm;
      this.$emit('getCategoryId', {id:category3Id,flag:3});
    }
  }
}
</script>

<style>

</style>
